#!/bin/sh
LICENCEFILE="my_licences"
[ ! -f "$LICENCEFILE" ] && echo "$LICENCEFILE is missing. Abort" && exit 1

ORI=$(sed -n 2p $LICENCEFILE | cut -c -13) 

FILELIST=$(find -name "*.cpp" -o -name "*.h")

for i in $FILELIST;
do
    [ "$(sed -n 2p $i | cut -c -13)" == "$ORI" ] && echo "$i is OK" && continue

    [ -f "$NEWFILE" ] && echo "Sorry $NEWFILE already exists" && continue

    NEWFILE="${i}.new"
    cat $LICENCEFILE $i > $NEWFILE
    echo "Create $NEWFILE success!"
done


for i in $(find -name "*.new")
do
    mv "$i" "${i%.new}";
    echo "Convert ${i%.new} OK!!"
done
